#include "plt.h"
#include <QFile>
#include <QTextStream>

CPlt::CPlt()
{
}


void CPlt::load(QString filename, QList<QVector3D*> &data, QRectF &rect)
{
  QFile file(filename);
  if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
     return;

   QTextStream in(&file);
   QString line = in.readLine();
   data.clear();
   while (!line.isNull())
   {
     if(line.startsWith("PU") || line.startsWith("PD"))
     {
       qreal z;
       if(line.startsWith("PU")) z = 0; else z = 1;
       line.remove(0,2);
       line.chop(1);
       QStringList sl = line.split(" ");
       QVector3D *nv = new QVector3D(sl.at(0).toDouble(),-sl.at(1).toDouble(),z);
       if(data.isEmpty())
       {
         rect = QRectF(nv->x(), nv->y(), 0,0);
       }
       else
       {
         if(nv->x() < rect.left()) rect.setLeft(nv->x());
         if(nv->y() < rect.top()) rect.setTop(nv->y());
         if(nv->x() > rect.right()) rect.setRight(nv->x());
         if(nv->y() > rect.bottom()) rect.setBottom(nv->y());
       }
       data.append(nv);
     }
     line = in.readLine();
   }
   for(int i = 0; i < data.size(); i++)
   {
     QVector3D *v = data.at(i);
     v->setX(v->x() - rect.left());
     v->setY(v->y() - rect.top());
   }
}
